/*************************************************************************
	> File Name: 007.P1507.NASA的食物计划_空间优化版.cpp
	> Author: Maureen 
	> Mail: Maureen@qq.com 
	> Created Time: 二  6/29 12:57:47 2021
 ************************************************************************/

#include <iostream>
using namespace std;

int V, M, N, v[50], m[50], c[50], ans[405][405];

int main() {
    cin >> V >> M >> N;
    for (int i = 1; i <= N; i++) {
        cin >> v[i] >> m[i] >> c[i];
    }

    for (int i = 1; i <= N; i++) {
        for (int j = V; j >= v[i]; j--) {
            for (int k = M; k >= m[i]; k--) {
                ans[j][k] = max(ans[j][k], ans[j - v[i]][k - m[i]] + c[i]);
            }
        }
    }
    cout << ans[V][M] << endl;
    return 0;
}
